package com.javacodegeeks.android.googlemaps;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.location.Address;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.location.Geocoder;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


public class GMapsActivity extends MapActivity {
	
	private MapView mapView;
	
//	private static final int latitudeE6 = 37985339;
//	private static final int longitudeE6 = 23716735;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mapView = (MapView) findViewById(R.id.map_view);       
        mapView.setBuiltInZoomControls(true);
        
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
		CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
		
		// *********************** getting latitude by address
		// display google map
		// position by address
		// hint on each tag
		// --------------to be done next
		// 1 read address and hints from XML file
		// deploy app on mobile with new key
		 Geocoder geoCoder = new Geocoder(this, Locale.getDefault()); 
		 GeoPoint p1;
		 GeoPoint p2;
	        try {
	            List<Address> add1 = geoCoder.getFromLocationName(
	                "58, rue haute, deuil la barre, france", 5);
	            List<Address> add2 = geoCoder.getFromLocationName(
		                "1, rue de la gare, deuil la barre, france", 5);

	            if (add1.size() > 0) {
	            	p1 = new GeoPoint(
	                        (int) (add1.get(0).getLatitude() * 1E6), 
	                        (int) (add1.get(0).getLongitude() * 1E6));
	            	p2 = new GeoPoint(
	                        (int) (add2.get(0).getLatitude() * 1E6), 
	                        (int) (add2.get(0).getLongitude() * 1E6));
	            	// begin hint
	            	OverlayItem overlayitem1 = new OverlayItem(p1, "Hello", "you are in 58 rue haute deuil la barre!");
	        		itemizedOverlay.addOverlay(overlayitem1);
	            	OverlayItem overlayitem2 = new OverlayItem(p2, "Hello", "you are rue de la gare, deuil la barre!");
	        		itemizedOverlay.addOverlay(overlayitem2);	        		
	        		mapOverlays.add(itemizedOverlay);
	            	// end hint
	            	MapController mc = mapView.getController();
	                mc.animateTo(p1); 
	        		mc.setZoom(6);
	                mapView.invalidate();
	            }    
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
		// ***********************
		
//		GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
//		OverlayItem overlayitem = new OverlayItem(point, "Hello", "I'm in Athens, Greece!");
		
//		itemizedOverlay.addOverlay(overlayitem);
//		mapOverlays.add(itemizedOverlay);
//		
//		MapController mapController = mapView.getController();
//		
//		mapController.animateTo(point);
//		mapController.setZoom(6);
        
    }

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
    
}